package com.td.erhome.ui.sellhouse;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.td.erhome.R;
import com.td.erhome.base.BaseActivity;
import com.td.erhome.models.PayMethod;
import com.td.erhome.utils.ConstUtils;
import com.td.erhome.utils.DialogUtils;
import com.td.erhome.utils.PayUtils;
import com.td.erhome.utils.RxBus;
import com.td.erhome.utils.UserManager;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

public class ContinueActivity extends BaseActivity {


    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.tv_publish)
    TextView mTvPublish;
    @Bind(R.id.img_sure)
    ImageView mImgSure;
    private String mHouse_id;
    private DialogUtils mDialogUtils;
    private Subscription mSubscribe;
    private Subscription mSubscribe1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_continue;
    }

    @Override
    protected void initView() {
        mHouse_id = getIntent().getStringExtra("house_id");
        mDialogUtils = new DialogUtils();

        mSubscribe = RxBus.getInstance().toObserverable(PayMethod.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<PayMethod>() {
                    @Override
                    public void call(PayMethod payMethod) {
                        sellHousePublish(payMethod.pay_type);
                    }
                });

        mSubscribe1 = RxBus.getInstance().toObserverable(Integer.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer i) {
                        if (i== ConstUtils.FINISH_CON){
                            Intent intent = new Intent(ContinueActivity.this,PublishFinishActivity.class);
                            intent.putExtra("house_id",mHouse_id);
                            startActivity(intent);
                            finish();
                    }}
                });
    }

    @Override
    protected void releaseResource() {
        mDialogUtils=null;
        if (!mSubscribe.isUnsubscribed()) {
            mSubscribe.unsubscribe();
        }if (!mSubscribe1.isUnsubscribed()) {
            mSubscribe1.unsubscribe();
        }
    }

    @OnClick({R.id.back, R.id.tv_publish, R.id.img_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tv_publish:
                mDialogUtils.initPopGetInfo(this,2);
                break;
            case R.id.img_sure:
                Intent intent = new Intent(this, SecondActivity.class);
                intent.putExtra("house_id",mHouse_id);
                startActivity(intent);
                break;
        }
    }

     //获取预支付信息
    public void sellHousePublish(String pay_type){
        PayUtils payUtils = new PayUtils(this);
        HashMap<String, Object> params = new HashMap<>();
        params.put("house_id", mHouse_id);
        params.put("pay_type",pay_type);
        params.put("user_id", UserManager.getIns().getUser().uid);
        payUtils.getPrePay(params);
    }

}
